package client;

import entry.Member;

import java.awt.*;
import java.awt.event.*;

public class Client {
    private String IP="localhost";
    private Member member;

    private TextField IDField;
    private TextField nameField;
    private TextArea receiveArea;
    private TextArea sendArea;
    private Button connectButton;
    private Button sendButton;
    private Frame mainWindow;
    private ClientManager manager;
    public static void main(String[] args) {
        try {
            Client client = new Client();
            client.window();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void window(){
        member = new Member();
        IDField = new TextField("请输入房间ID");
        nameField = new TextField("请输入用户名");
        connectButton = new Button("连接");
        connectButton.addActionListener(new ConnectButtonHandler());
        receiveArea = new TextArea();
        receiveArea.setEditable(false);
        sendArea = new TextArea();
        sendArea.setEditable(false);
        sendButton = new Button("发送");
        sendButton.setEnabled(false);
        sendButton.addActionListener(new SendButtonHandler());


        manager = ClientManager.getClientManager();
        manager.setReceiveArea(receiveArea);
        mainWindow = new Frame("Client");
        mainWindow.setBounds(100,100,560,500);
        mainWindow.add(IDField);
        mainWindow.add(nameField);
        mainWindow.add(connectButton);
        mainWindow.add(receiveArea);
        mainWindow.add(sendArea);
        mainWindow.add(sendButton);
        mainWindow.setVisible(true);

        FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT,20,20);
        mainWindow.setLayout(flowLayout);

        sendArea.addTextListener(e -> {
            if (sendArea.getText().trim().equals("")){
                sendButton.setEnabled(false);
            }
            else{
                sendButton.setEnabled(true);
            }
        });

        mainWindow.addWindowListener(new WindowAdapter() {
            //窗体点击关闭时，要做的事
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }

    class ConnectButtonHandler implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            if (connectButton.getLabel().equals("连接")){
                member.setID(IDField.getText());
                member.setUsername(nameField.getText());
                manager.connect(IP,member);
                connectButton.setLabel("中断");
                IDField.setEditable(false);
                nameField.setEditable(false);
                sendArea.setEditable(true);
            }
            else{
                manager.disconnect();
                IDField.setEditable(true);
                nameField.setEditable(true);
                sendArea.setEditable(false);
                connectButton.setLabel("连接");
            }
        }
    }

    class SendButtonHandler implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            manager.send(sendArea.getText());
            sendArea.setText(null);
        }
    }
}


